
import com.dx.common.table.ppt.Spc_mfg_data;
import com.dx.mes.common.dao.ISpcMfgDataDao;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created by xxd on 2017/9/28.
 */
public class InsertIntoTable {

    @Autowired
    private ISpcMfgDataDao spcMfgDataDao;

    @Test
    public void test() throws Exception {
        // TODO Auto-generated method stub
        File file = new File("E:/TOOL/p170927.d11");
        BufferedReader reader = null;
        String tempString = null;
        int line =1;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            while ((tempString = reader.readLine()) != null) {
                String temp = "";
                String[] tep;
                Spc_mfg_data spc_mfg_data = new Spc_mfg_data();
                if(tempString.contains("T_Spike")){
                    temp = tempString.substring(41);
                    tep = temp.split("  ");
                    spc_mfg_data.setTool_id("DIF1-1");
                    spc_mfg_data.setMes_id("TEMP");
                    for(int i = 0 ;i < tep.length ;i++){
                        String prop = String.format("data_value_%d", i + 1);
                        try {
                            BeanUtils.setProperty(spc_mfg_data, prop,tep[i]);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    spcMfgDataDao.save(spc_mfg_data);
                }else if(tempString.contains("ActPwr")){
                    temp = tempString.substring(40);
                    tep = temp.split("  ");
                    spc_mfg_data.setTool_id("DIF1-1");
                    spc_mfg_data.setMes_id("POWR");
                    for(int i = 0 ;i < tep.length ;i++){
                        String prop = String.format("data_value_%d", i + 1);
                        try {
                            BeanUtils.setProperty(spc_mfg_data, prop,tep[i]);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    spcMfgDataDao.save(spc_mfg_data);
                }else if(tempString.contains("N2")){
                    temp = tempString.substring(49);
                    tep = temp.split("  ");
                    spc_mfg_data.setTool_id("DIF1-1");
                    spc_mfg_data.setMes_id("FLOW");
                    for(int i = 0 ;i < tep.length ;i++){
                        String prop = String.format("data_value_%d", i + 1);
                        try {
                            BeanUtils.setProperty(spc_mfg_data, prop,tep[i]);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    spcMfgDataDao.save(spc_mfg_data);
                }else if(tempString.contains("Press tube")){
                    temp = tempString.substring(57);
                    tep = temp.split("  ");
                    spc_mfg_data.setTool_id("DIF1-1");
                    spc_mfg_data.setMes_id("PRES");
                    for(int i = 0 ;i < tep.length ;i++){
                        String prop = String.format("data_value_%d", i + 1);
                        try {
                            BeanUtils.setProperty(spc_mfg_data, prop,tep[i]);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    spcMfgDataDao.save(spc_mfg_data);
                }

                System.out.println("Line"+ line + ":" +tempString);
                line ++ ;
            }
            reader.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }


   /* public String getDBDataValue(Integer i, Spc_mfg_data spc_mfg_data) {
        String data_value = "";
        String prop = String.format("data_value_%d", i + 1);
        try {
            data_value = BeanUtils.getProperty(spc_mfg_data, prop);
            data_value = BeanUtils.setProperty(spc_mfg_data, prop,);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return data_value;
    }*/


}
